/**
 * 
 */
package com.wolfpire.section.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.wolfpire.chapter.dao.SubjectChapterRelationDao;
import com.wolfpire.knowledge.dao.SubjectKnowledgeRelationDao;
import com.wolfpire.section.dao.ChapterSectionRelationDao;
import com.wolfpire.subject.dao.ProfessionSubjectRelationDao;
import com.wolfpire.system.common.base.dao.impl.BaseHibernateDao;
import com.wolfpire.system.dao.OrgUserRelationDao;
import com.wolfpire.system.model.ChapterSectionRelation;
import com.wolfpire.system.model.OrgUserRelation;
import com.wolfpire.system.model.ProfessionSubjectRelation;
import com.wolfpire.system.model.SubjectChapterRelation;
import com.wolfpire.system.model.SubjectKnowledgeRelation;

/**
 * @author lihd
 *
 */

@Repository("chapterSectionRelationDao")
public class ChapterSectionRelationDaoImpl extends BaseHibernateDao<ChapterSectionRelation, Long> implements ChapterSectionRelationDao {

	public void relate(Long chapterId, Long sectionId) {
		//先删除后绑定
				this.deleteBysectionId(sectionId);
				if (null != chapterId) {
					ChapterSectionRelation cs = new ChapterSectionRelation(sectionId,chapterId);
					this.saveOrUpdate(cs);
				}
	}
	
	private void deleteBysectionId(Long sectionId) {
		String executeSql = "DELETE FROM t_chapter_section WHERE section_id = ?";
		this.executeSql(executeSql, sectionId);
			
	}

	public ChapterSectionRelation getCSRelation(Long sectionId) {
		Criteria c = createCriteria(Restrictions.eq("sectionId", sectionId));
		Object obj = c.uniqueResult();
		if (null != obj) {
			return (ChapterSectionRelation) obj;
		}
		return null;
	}

	public List<ChapterSectionRelation> getSCRelations(Long chapterId) {
		Criteria c = createCriteria(Restrictions.eq("chapterId", chapterId), Restrictions.isNotNull("chapterId"));
		return c.list();
	}


}
